<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/flash/savepagedata.php
#	FUNCTION:		Saves the content of a page
#	AUTHOR:			Cameron Morrow
#	CREATED:		11/08/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Report all errors
error_reporting(E_ALL);

# Start session
session_start();

# Use this for IE to remember form data correctly
header("Cache-control: private");


header("content-type: text");

# Vars
$ERROR_ARRAY = array();
$SQL_ARRAY = array();

# Includes
include "../../site_specific/project.php";
include "../../includes/constants.php";
include "../../includes/utilities.php";
include "../../includes/pages.php";
include "../../includes/database.php";
include "../../includes/users.php";
include "../../includes/files.php";
include "../../includes/date.php";
include "../../includes/text.php";

# Establish database connection
establishDatabaseConnection();

# Get ID
$PAGE_ID = (@$_POST["page"]) ? $_POST["page"] : "";

# Get Type
$TYPE = (@$_POST["type"]) ? $_POST["type"] : "page";

# Get content
$PAGE_CONTENT = (@$_POST["content"]) ? $_POST["content"] : "";

# Check for bad ID
if ($PAGE_ID == "") {

	echo "savesuccess=false&error=No Page ID passed";

# Otherwise ...
} else {

	# Save checkpoint
	# $save = createFile("../", "save0-log-" . getFullDate() . "-" . rand(100, 999) . ".txt", flash_unescape($PAGE_CONTENT));

	# -----------------------------------------------------------------------------------
	# H A N D L E   E N C O D I N G
	# -----------------------------------------------------------------------------------

	# Split up into encoded elements
	$PAGE_CONTENT = explode(">%", $PAGE_CONTENT);
	
	# For each item
	foreach ($PAGE_CONTENT as $pc => $content) {

		# See if a % needs to be added again, or if an ending > needs to be added
		$content = ($pc > 0) ? "%" . $content : $content;
		$content .= ($pc < count($PAGE_CONTENT) - 1) ? ">" : "";

		# If there's encoded content to be had
		if ($pc > 0) {

			# Get encoded section and non-encoded section
			$encoded = substr($content, 0, strpos($content, "<"));
			$unencoded = substr($content, strlen($encoded));

			# Decode
			$decoded = flash_unescape($encoded);

			# Fix Fonts
			$decoded = preg_replace(
				array(
					"/<FONT FACE=\"[^\"]*\"/i"
				),
				array(
					"<FONT FACE=\"Verdana\""
				),
				$decoded);

			# Content
			$content = flash_escape($decoded) . $unencoded;
			
			
			
		}
		
		//src="../assets/uploads/7001m7_img_4c.jpg"

		# Insert back into array
		$PAGE_CONTENT[$pc] = $content;
	}

	# Merge back together again
	$PAGE_CONTENT = implode("", $PAGE_CONTENT);

	# Remove any slashes
	$PAGE_CONTENT = stripslashes($PAGE_CONTENT);

	# Append header
	$PAGE_CONTENT = "<?phpxml version=\"1.0\" encoding=\"iso-8859-1\"?>" . $PAGE_CONTENT;


	# -----------------------------------------------------------------------------------
	# X S L   P A R S I N G
	# -----------------------------------------------------------------------------------

	# Run through XSL parser
	$arguments_flash = array(
		"/_xml" => $PAGE_CONTENT,
		"/_xsl" => file_get_contents("flash.xsl")
	);

	# Create XSLT Processor
	$xsltproc_flash = xslt_create();

	# Convert
	$flash_content = xslt_process(
		$xsltproc_flash,
		'arg:/_xml',
		'arg:/_xsl',
		null,
		$arguments_flash
	);

	# Finish
	xslt_free($xsltproc_flash);


	# -----------------------------------------------------------------------------------
	# S A V E   D A T A
	# -----------------------------------------------------------------------------------

	if ($TYPE == "page") {

		//$flash_content=str_replace("src%3D%22images%2F","src%3D%22..%2Fassets%2Fuploads%2F",$flash_content);
		//$flash_content=str_replace("%3CFONT%20size%3D%22%2B2%22%3E","",$flash_content);
		//$flash_content=str_replace("%3CFONT%20size%3D%22%2B1%22%3E","",$flash_content);
		# Create SQL
		$sql = "UPDATE " . $PROJECT_DB_TABLES["pages"] . " SET p_content = '" . $flash_content . "', p_modified = '" . getFullDate() . "' WHERE p_id = '" . $PAGE_ID . "'";
	} else {

		# Create SQL
		$sql = "UPDATE " . $PROJECT_DB_TABLES["newsletters"] . " SET n_content = '" . $flash_content . "', n_modified = '" . getFullDate() . "' WHERE n_id = " . $PAGE_ID;
	}

	# Attempt to execute
	$successful_save = executeSQLQuery($sql);
	echo "savesuccess=true";
	if ($successful_save) {
		
		//echo "savesuccess=true";
		# Log
		if ($TYPE == "page") {
			addLog($PAGE_ID, 10, 4, @$_SESSION[$PROJECT_CODE . "id"]);
		} else {
			addLog($PAGE_ID, 28, 4, @$_SESSION[$PROJECT_CODE . "id"]);
		}
	} else {
		//echo "savesuccess=false&error=PHP Error: " . print_r($ERROR_ARRAY[0], true);
	}
}

?>